package com.woniuxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.dao.CurrentUnitMapper;
import com.woniuxy.pojo.CurrentUnit;
import com.woniuxy.pojo.CurrentUnit;
import com.woniuxy.service.CurrentUnitService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.vo.CurrentUnitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CurrentUnitServiceImpl implements CurrentUnitService{
    @Autowired
    private CurrentUnitMapper currentUnitMapper;

    @Override
    public DataGridView getCurrentUnitListByPage(CurrentUnitVo currentUnitVo) {
        Page<Object> page= PageHelper.startPage(currentUnitVo.getPage(),currentUnitVo.getLimit());
        List<CurrentUnit> currentUnitList=this.currentUnitMapper.selectCurrentUnitByProperty(currentUnitVo);
        return new DataGridView(page.getTotal(),currentUnitList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addCurrentUnit(CurrentUnit currentUnit) {
        boolean flag=false;
        int count=this.currentUnitMapper.insertCurrentUnit(currentUnit);
        if(count>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public CurrentUnit findCurrentUnitByPhone(String phone) {
        return this.currentUnitMapper.findCurrentUnitByPhone(phone);
    }

    @Transactional
    @Override
    public void removeCurrentUnit(int id) {
        this.currentUnitMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    @Override
    public void removeBathCurrentUnit(Integer[] ids) {
        if(ids.length>0){
            for(Integer id:ids){
                this.removeCurrentUnit(id);
            }
        }
    }

    @Override
    public boolean update(CurrentUnit currentUnit) {
        int res = currentUnitMapper.update(currentUnit);
        if(res>0){
            return true;
        }
        return false;
    }
}
